<?php
class MenuController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list menu
		$args = array();
		$arrMenu = Menu::getInstance()->getList($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrMenu[0]['total_rows'])?$arrMenu[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('index', array(
			'arrMenu' => $arrMenu,
			'objPages' => $objPages,
			'args' => $args
		));
	}
	
	public function actionForm()
	{
		$arrItemInfo = array();
		$intId = Yii::app()->request->getParam('id', null);
		//if post data
		if(Yii::app()->request->isPostRequest)
		{
			$arrItemInfo = Yii::app()->request->getParam('menu', null);
			//check if aldready code
			$item = Menu::getInstance()->getByCode($arrItemInfo['menu_name']);
			if( (empty($intId) && !empty($item)) || (!empty($intId) && $intId != $item['menu_id']) ){
				Yii::app()->admin->setFlash('error', 'The code is already in use. It must be unique');
			} else{
				if(empty($intId)){
						//insert
						Menu::getInstance()->insert($arrItemInfo);
				}
				else{
					//update
					Menu::getInstance()->update($intId, $arrItemInfo);
				}
			}
		}
		//load form
		if(!empty($intId)){
			$arrItemInfo = Menu::getInstance()->get($intId);
		}
		//render layout
		$this->render('form', array(
			'intId' => $intId,
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Menu::getInstance()->delete($intId);
		header('Location: '.LINK_ADMINCP_USERS);
	}
	
	public function actionItems(){
		$intMenuId = Yii::app()->request->getParam('menu', null);
		if(!empty($intMenuId)){
			$arrMenuItems = Menu::getInstance()->listItems($intMenuId);	
			//render layout
			$this->render('items', array(
				'intMenuId' => $intMenuId,
				'arrMenuItems' => $arrMenuItems
			));
		}
	}
	
	public function actionUpdateItem(){
		$intMenuId = Yii::app()->request->getParam('menu', null);
		if(!empty($intMenuId)){
			
			//if post data
			if(Yii::app()->request->isPostRequest){
				$intId = Yii::app()->request->getParam('id', null);
				$arrItemInfo = Yii::app()->request->getParam('item', null);
				if(empty($intId)){
					Menu::getInstance()->insertItem($arrItemInfo);
				}
				else{
					Menu::getInstance()->updateItem($intId, $arrItemInfo); 
				}
				
			}
			
			//load data
			$arrMenuItems = Menu::getInstance()->listItems($intMenuId);	
			//render layout
			$this->render('item-form', array(
				'intMenuId' => $intMenuId,
				'arrMenuItems' => $arrMenuItems
			));
		}
	}
	
	public function actionUpdateItemsHierarchy(){
		$arrParams = Yii::app()->request->getParam('ul', null); 
		if(Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest){
			$args = $this->buildHierarchy($arrParams);;
			Menu::getInstance()->updateHierarchy($args);
			if($arrParams) {
				print_r($args);
				print_r($arrParams); die;
			}
		}
	}
	
	private function buildHierarchy(array $arrParams, &$tree = array()) {
		foreach($arrParams as $R){
			if(!empty($R['children'])){
				foreach($R['children'] as $child)
					$tree[$R['id']][] = $child['id'];
					$this->buildHierarchy($R['children'], $tree);
			} else {
				$tree[$R['id']] = array();
			}
		}
		return $tree;
	}

}